
window.indexedDB=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB;
window.IDBTransaction=window.IDBTransaction||window.webkitTransaction||window.msTransaction;
window.IDBKeyRange=window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange;
window.IDBCursor=window.IDBCursor||window.webkitIDBCursor||window.msIDBCursor;
var dbName='Data';
var dbVersion='2017';
var idb,datatable;
var data;
//页面加载时执行
function window_onload() {
    var dbConnect=window.indexedDB.open(dbName,dbVersion);
    dbConnect.onsuccess=function (e) {
        idb = e.target.result;
    };
    dbConnect.onerror=function () {
        alert("连接数据库失败");
    };
    //内置admin管理员信息
    const adminData = {name:"admin",pwd:"admin"}

    //数据库版本更新后执行
    dbConnect.onupgradeneeded=function (e) {
        idb = e.target.result;
        var tx=e.target.transaction;
        tx.onabort=function (e) {
            alter('创建对象仓库失败')
        };
        var storename1='orders';    //订单仓库
        var storename2='customer';  //客户仓库
        var storename3='adms';      //管理员仓库
        var storename4='message';   //留言仓库
        var optionalParameters={
            keyPah:'id',
            autoIncrement:true                           //id自动编号（唯一）
        };
        var store1 =idb.createObjectStore(storename1,optionalParameters);//创建表
        var store2 =idb.createObjectStore(storename2,optionalParameters);
        var store3 =idb.createObjectStore(storename3,optionalParameters);
        var store4 =idb.createObjectStore(storename4,optionalParameters);
        alert('创建对象仓库成功');
        var indexname1='codeIndex';
        var keyPath1='code';
        var indexname2='nameIndex';
        var keyPath2='name';
        var indexname3='noIndex';
        var keyPath3='name';
        var indexname4='nameIndex';
        var keyPath4='name';
        var optionalParameters={
            unique:true,
            multiEntry:false
        };
        var idx1=store1.createIndex(indexname1,keyPath1,optionalParameters);
        var idx2=store2.createIndex(indexname2,keyPath2,optionalParameters);
        var idx3=store3.createIndex(indexname3,keyPath3,optionalParameters);
        store3.add(adminData);
        var idx4=store4.createIndex(indexname4,keyPath4,optionalParameters);
        alert('创建索引成功');
    };
}
//登录验证模块,单击登录按钮时执行
function btnLogin_onclick() {
    data = new Object();
    data.Name = document.getElementById("txtName").value;
    data.Pwd = document.getElementById("txtPwd").value;
    var tx = idb.transaction(['adms'], "readonly");//创建事务
    var store = tx.objectStore('adms');

    var idx=store.index('noIndex');
    var range = IDBKeyRange.lowerBound(1);
    var direction="next";
    var req =idx.openCursor(range,direction);
    var i=0;
    req.onsuccess=function () {
        var cursor = this.result;
        if (cursor) {
            i += 1;
            if(cursor.value.pwd==data.Pwd&&cursor.value.name==data.Name){
                alert("登陆成功！");
                document.getElementById("funcid").disabled=false;
                self.location="pages/menu.html";
            }
            else{
                cursor.continue();
            }
        }
        else{
            alert("账号或密码错误！");
        }
    }
}
//重置用户信息，单击重置按钮时执行
function btnClear_onclick() {
    document.getElementById("txtName").value="";
    document.getElementById("txtPwd").value="";
}
//用户注册模块，单击注册按钮时执行
function btnRegister_onclick() {
    data = new Object();
    data.Name=document.getElementById("txtName").value;
    data.Pwd=document.getElementById("txtPwd").value;
    var tx=idb.transaction(['adms'],"readwrite");

    var chkErrorMsg="";
    tx.oncomplete=function () {          //在追加数据以后执行
        if(chkErrorMsg!="")
            alert(chkErrorMsg);
        else{
            alert('注册成功');
            btnClear_onclick();
        }
    }
    tx.onabort=function () {
        alert('注册失败');
    }

    var store=tx.objectStore('adms');
    var idx=store.index('noIndex');
    var range = IDBKeyRange.only(data.Name);
    var direction="next";
    var req =idx.openCursor(range,direction);
    req.onsuccess=function () {
        var cursor=this.result;
        if(cursor){
            if(data.Name==cursor.value.name)
                chkErrorMsg="账号已经存在！";       //账号不可重复注册
        }
        else{
            var value ={
                name:data.Name,
                pwd:data.Pwd
            };
            store.put(value);
        }
    }
    req.onerror=function () {
        alert('注册失败');
    }


}

